﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml.Data;

namespace WinRTConverters.Media
{
    /// <summary>
    /// Permet de transformer une chaine du type ARGB en <see cref="Color"/>
    /// </summary>
    public class StringARGBToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value != null)
            {
                return new Color
                {
                    A = byte.Parse(value.ToString().Substring(1, 2), NumberStyles.AllowHexSpecifier),
                    R = byte.Parse(value.ToString().Substring(3, 2), NumberStyles.AllowHexSpecifier),
                    G = byte.Parse(value.ToString().Substring(5, 2), NumberStyles.AllowHexSpecifier),
                    B = byte.Parse(value.ToString().Substring(7, 2), NumberStyles.AllowHexSpecifier),
                };
            }
            return Colors.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return ((Color) value).ToString();
        }
    }
}
